using Slaks.Filters.Extended;
using Slaks.Web.Parser;
using TextParsing;
namespace Slaks.TextParsing
{
using System;
using System.IO ;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary ;
using System.Windows.Forms ;
using Slaks.Readers;
using Slaks.Filters;

	public class MDIDoc
	{
		public int docID; // ID to display it on the view
		public MDIView viewList = null;

		private string m_outputPath = String.Empty;
		private System.Text.Encoding m_encoding = null;
		private IList m_filesToParse = null;

		private Type m_bufferReader = null;
		private BufferReader m_bufferReaderCore = null;
		private Type m_filter = null;
		private AFilter m_filterCore = null;
		private FileReader m_stopwordCollectionFileReaderCore = null;
		private Type m_stopwordCollectionFileReader = null;

		private Type m_stopwordCollectionFilter = null;
		private AFilter m_stopwordCollectionFilterCore = null;


		private string m_stopwordCollectionFileName = String.Empty;
		private bool m_isUseReaderFilterModules = false;
		private static bool m_isUseStopwordCollectionModule = false;
		private static StopwordCollection m_stopwordCollection = null;

		private StructureConstruction m_structureConstruction;
	
		public MDIDoc(Form1 mainWin)
		{
			//Set the ID to be the current count + 1. This is so that the ID starts with 1
			docID = Form1.documentCount+1;
			//Create a view (Form) for this document
			MDIView view = new MDIView(this,mainWin);
			viewList = view;
			view.Show();				
		}

		#region unused
		//Save the document
		public void SaveDocument(string fileName)
		{
		}

		//Open the document
		public void OpenDocument(string fileName)
		{
		}	
		#endregion

		//Updates all the views of the document with the new data
		public void UpdateAllViews(MDIView sender)
		{		
			MDIView view;
			view = (MDIView)viewList;
			if(view.Equals(sender))	return;
			view.Update();
		}

		// Deletes the contents of the document
		public void DeleteContents()
		{
			UpdateAllViews(null); // sync everything
		}

		public Type BufferReader
		{
			get { return m_bufferReader; }
			set { m_bufferReader = value; }
		}

		public Type Filter
		{
			get { return m_filter; }
			set { m_filter = value; }
		}

		public Type StopwordCollectionFileReader
		{
			get { return m_stopwordCollectionFileReader; }
			set { m_stopwordCollectionFileReader = value; }
		}

		public string StopwordCollectionFileName
		{
			get { return m_stopwordCollectionFileName; }
			set { m_stopwordCollectionFileName = value; }
		}

		public bool ISUseReaderFilterModules
		{
			get { return m_isUseReaderFilterModules; }
			set { m_isUseReaderFilterModules = value; }
		}

		public static bool ISUseStopwordCollectionModule
		{
			get { return m_isUseStopwordCollectionModule; }
			set { m_isUseStopwordCollectionModule = value; }
		}

		public Type StopwordCollectionFilter
		{
			get { return m_stopwordCollectionFilter; }
			set { m_stopwordCollectionFilter = value; }
		}

		public IList FilesToParse
		{
			get { return m_filesToParse; }
			set { m_filesToParse = value; }
		}
		public string OutputPath
		{
			get { return m_outputPath; }
			set { m_outputPath = value; }
		}

		public System.Text.Encoding Encoding
		{
			get { return m_encoding; }
			set { m_encoding = value; }
		}

		public void ProceedParsing()
		{
    
			IList filesToParse = this.FilesToParse;
			try
			{
				if (this.ISUseReaderFilterModules)
				{
					InstantiateReadersAndModules();

					if (this.ISUseReaderFilterModules && ISUseStopwordCollectionModule)
					{
						this.InstantiateStopwordCollection();
					}
				}

				m_structureConstruction = new StructureConstruction(m_bufferReaderCore,this.OutputPath,this.Encoding,ISUseStopwordCollectionModule);
				m_structureConstruction.ReportProgressEvent += new Slaks.ParserUtil.ParserHandler(viewList.ReportProgressEventHandler);	
				m_structureConstruction.ProceedParsing(filesToParse);

			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message + "\n" + ex.StackTrace,"Parsing error",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void InstantiateStopwordCollection()
		{
			try
			{
				m_stopwordCollectionFileReaderCore = (FileReader)Activator.CreateInstance(m_stopwordCollectionFileReader,
					new object [] {m_stopwordCollectionFileName,m_encoding});
				if (m_stopwordCollectionFileReaderCore == null) throw new Exception(m_stopwordCollectionFileReader.Name + " instantiation error");
				m_stopwordCollection = StopwordCollection.CreateInstance(m_stopwordCollectionFileReaderCore,m_filterCore);
				if (m_stopwordCollection == null) throw new Exception("Stopword collection loading error");

				m_stopwordCollectionFilterCore = (AFilter) Activator.CreateInstance(m_stopwordCollectionFilter);
				if (m_stopwordCollectionFilterCore == null) throw new Exception(m_stopwordCollectionFilter.Name + " instantiation error");

			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message,"Stopword collection module instantiation error",MessageBoxButtons.OK,MessageBoxIcon.Error);
				ISUseStopwordCollectionModule = false;
			}
		}

		private void InstantiateReadersAndModules()
		{ 
			try
			{
				m_filterCore = (AFilter) Activator.CreateInstance(m_filter);
				if (m_filterCore == null) throw new Exception(m_filter.Name + " instantiation error");
				m_bufferReaderCore = (BufferReader) Activator.CreateInstance(m_bufferReader,new object[]{m_filterCore});
				m_bufferReaderCore.IsOutputPunctuationMarks = true;
				m_bufferReaderCore.IsOutputSpecialCharacter = false;
			
				if (m_bufferReaderCore == null) throw new Exception(m_bufferReader.Name + " instantiation error");
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message,"Module instantiation error",MessageBoxButtons.OK,MessageBoxIcon.Error);
				this.ISUseReaderFilterModules = false;
			}
		}

		internal static bool ISStopword(string term)
		{
			if (ISUseStopwordCollectionModule == false) return false;

			return m_stopwordCollection.Contains(term);
		}
	}
}
